const app = getApp();
const https = require('../../utils/https');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderIndex: 0, // 商品
    orderState: '',
    type: 'ALL',
    order_list: [],
    orderList: []
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options && options.type != '') {
      this.setData({
        type: options.type
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
    var that = this,
    login_status = wx.getStorageSync('login_status');
    that.setData({
      page: 0,
      order_list: []
    })
    if (!login_status || login_status <= 0) {
      wx.showLoading({
        title: '请先进行登陆',
      })
      setTimeout(() => {
        wx.hideLoading({
          success: (res) => {
            wx.navigateTo({
              url: '/pages/grant/grant',
            })
          },
        })
      }, 1500);
    }else{
      that.getOrderType();
      that.getOrderList();
    }
  },
  /**
   * @xk 订单类型 
   */
  orderTap: function (e) {
    this.setData({
      type: e.currentTarget.dataset.type,
      page: 0,
      order_list: []
    })
    this.getOrderList();
  },
  /**
   * @xk 获取订单类型
   */
  getOrderType(){
    var that = this;
    https.post('getOrderType',{},true).then(res=>{
      that.setData({
        order_type: res
      })
    })
  },
  /**
   * @xk 获取订单
   */
  getOrderList: function () {
    var that = this,
      type = this.data.type,
      page = this.data.page,
      user_id = wx.getStorageSync('user_id'),
      order_list = this.data.order_list;
    https.post('getOrderList', {
      type: type,
      page: page,
      user_id: user_id,
      is_page: 1
    }, false, '正在获取数据').then(res => {
      if(res.list){
        order_list.push.apply(order_list,res.list);
      }
      that.setData({
        order_list: order_list,
        page: res.page
      })
    })
  },
  /**
   * @xk 取消订单
   * @param {} obj 
   */
  cancel: function(obj){
    var order_sn = obj.currentTarget.dataset.order_sn;
    https.post('cancelOrder',{order_sn:order_sn},false,'正在取消').then(res=>{
      this.setData({
        order_list: [],
        page: 0
      })
      this.getOrderList();
    })
  },
  /**
   * @xk 提交支付
   * @param {*} obj 
   */
  goPay: function(obj){
    var { order_sn, order_id} = obj.currentTarget.dataset;
    https.post('goPay',{order_sn:order_sn},false,'正在重新支付').then(res=>{
      app.globalData.order_sn = res.order_sn;
      if(res.code == 205){
        //执行微信支付
        wx.requestPayment({
          timeStamp: res.pay_info.timeStamp,
          nonceStr: res.pay_info.nonceStr,
          package: res.pay_info.package,
          signType: res.pay_info.signType,
          paySign: res.pay_info.paySign,
          success (res) {
            if(res.errMsg == 'requestPayment:ok'){
              wx.showToast({
                title: '支付成功',
                icon: 'none',
                duration: 1500
              })
              wx.redirectTo({
                url: '/pages/pay_success/success?order_id=' + order_id,
              })
            }else{
              wx.showToast({
                title: '支付取消',
                icon: 'none',
                duration: 2000
              })
              wx.redirectTo({
                url: '/pages/my_order/my_order',
              })
            }
          },
          fail (res) { 
            wx.showToast({
              title: '支付取消 ',
              icon: 'none',
              duration: 1500
            })
            wx.redirectTo({
              url: '/pages/my_order/my_order',
            })
          }
        })
      }else{
        wx.redirectTo({
          url: '/pages/pay_success/success',
        })
      }
    })
  },
  /**
   * @xk 申请退款
   * @param {*} obj 
   */
  backMoney:function(obj){
    var order_sn = obj.currentTarget.dataset.order_sn;
    wx.navigateTo({
      url: '/pages/applyrefund/applyrefund?order_sn='+order_sn,
    })
  },
  /**
   * @xk 确认收货
   * @param {*} obj 
   */
  makeSure: function(obj){
    var order_sn = obj.currentTarget.dataset.order_sn;
    https.post('makeSure',{order_sn:order_sn},false,'正在确认收货').then(res=>{
      if(res){
        this.setData({
          order_list: [],
          page: 0
        })
        this.getOrderList();
      }
    })
  },
  common: function(obj){
    var order_sn = obj.currentTarget.dataset.order_sn;
    wx.navigateTo({
      url: '/pages/evaluate/evaluate?order_sn='+order_sn,
    })
  },
  goShow: function(obj){
    var order_sn = obj.currentTarget.dataset.order_sn;
    wx.navigateTo({
      url: '/pages/myorder_info/info?order_id='+order_sn,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      type: 'ALL',
      page: 0,
      order_list: []
    })
    this.getOrderList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getOrderList();
  },

})